<template>
  <section ref="contactRef" class="contact-section" :class="{ visible }">
    <!-- 内容区：表单 + 联系方式 -->
    <div class="contact-content">
      <!-- 表单区（左） -->
      <div class="contact-form">
        <h3>留下您的想法</h3>
        <p class="form-intro">
          无论是建议、合作机会或反馈意见，我们都欢迎您的声音。
        </p>
        <form @submit.prevent="submitForm">
          <input placeholder="您的姓名" v-model="form.Name" required />
          <input type="email" placeholder="邮箱地址" v-model="form.Email" />
          <input type="tel" placeholder="联系电话" v-model="form.Phone" />
          <textarea rows="5" placeholder="留言内容" v-model="form.MessageContent" required></textarea>
          <button type="submit">发送留言</button>
        </form>
      </div>

      <!-- 联系方式（右） -->
      <div class="contact-details">
        <div class="detail-item">
          <h4>公司地址</h4>
          <p>北京市朝阳区 灵感大厦 601 室</p>
        </div>
        <div class="detail-item">
          <h4>联系电话</h4>
          <p>400-123-4567<br />周一至周五 9:00 – 18:00</p>
        </div>
        <div class="detail-item">
          <h4>商务合作</h4>
          <p>partner@yourbrand.com</p>
        </div>
        <div class="detail-item">
          <h4>媒体咨询</h4>
          <p>media@yourbrand.com</p>
        </div>
      </div>
    </div>
  </section>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import http from '@/utils/http'

const visible = ref(false)
const contactRef = ref(null)

const form = ref({
  Name: '',
  Email: '',
  Phone: '',
  MessageContent: ''
})

const submitForm = async () => {
  try {
    await http.post('/contact', form.value)
    ElMessage.success('留言提交成功！我们会尽快联系您。')
    form.value = {
      Name: '',
      Email: '',
      Phone: '',
      MessageContent: ''
    }
  } catch (err) {
    console.error('提交失败', err)
    ElMessage.error('提交失败，请稍后再试')
  }
}

onMounted(() => {
  const observer = new IntersectionObserver(
    ([entry]) => {
      if (entry.isIntersecting) {
        visible.value = true
        observer.disconnect()
      }
    },
    { threshold: 0.3 }
  )
  if (contactRef.value) observer.observe(contactRef.value)
})
</script>

<style scoped>
.contact-section {
  opacity: 0;
  transform: translateY(60px);
  transition: all 1s ease;
  overflow: hidden;
}
.contact-section.visible {
  opacity: 1;
  transform: translateY(0);
}

.contact-content {
  display: flex;
  max-width: 800px;
  margin: 80px auto;
  padding: 0;
  height: 440px;
}
.contact-form,
.contact-details {
  width: 50%;
  box-sizing: border-box;
  padding: 10px 36px;
  border-radius: 10%;
  height: 450px;
}

.contact-details {
  padding: 40px;
  background: #f5f1ed;
  margin-left: 20px;
}
.contact-form {
  background: #fff;
}
.contact-form h3 {
  font-size: 30px;
  font-weight: 600;
  margin-bottom: 12px;
  margin-top: 0;
  color: #2e1c0e;
}
.form-intro {
  font-size: 14px;
  color: #666;
  margin-bottom: 24px;
  line-height: 1.6;
}
.contact-form input,
.contact-form textarea {
  width: 100%;
  font-size: 13px;
  padding: 14px 16px;
  margin-bottom: 16px;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
  color: #333;
  transition: border 0.3s ease;
}
.contact-form textarea {
  height: 60px;
}
.contact-form input:focus,
.contact-form textarea:focus {
  border-color: #c2956d;
  outline: none;
}
.contact-form button {
  background-color: #2e1c0e;
  color: #fff;
  font-size: 15px;
  padding: 12px 36px;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  transition: background 0.3s ease;
}
.contact-form button:hover {
  background-color: #502d13;
}
.contact-details h4 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #333;
}
.contact-details p {
  font-size: 14px;
  color: #555;
  line-height: 1.6;
  margin-bottom: 24px;
}
.detail-item {
  margin-left: 30px;
}
@media (max-width: 768px) {
  .contact-content {
    flex-direction: column;
    height: auto;
  }
  .contact-form,
  .contact-details {
    width: 100%;
    height: auto;
  }
}
</style>
